// components/uploadImg/index.js
import {
  toast
} from '../../utils/util'
import base from '../../api/base'
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: String,
    secondTitle: String,
    count: Number,
    imgUrl: {
      type: Array
    }
  },
  // 生命周期
  lifetimes: {
    ready() {
      setTimeout(() => {
        this.initImgList()
      }, 1000)
      // console.log(this.data.imgList.length)
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgList: [],
    imgPath: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initImgList() {
      const result = this.properties.imgUrl
      console.log(result)
      this.setData({
        imgList: result
      })
    },
    binduploadImg() {
      this.chooseImg()
    },
    chooseImg() {
      app.globalData.imgUrl = []
      this.setData({
        imgPath: []
      })
      const count = this.properties.count
      const _this = this
      wx.chooseImage({
        count,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          const result = []
          result.push(...res.tempFilePaths)
          _this.setData({
            imgList: result
          })
          app.globalData.imgUrlLen = result.length
          _this.setData({
            imgPath: result
          })
          _this.uploadImg()
        }
      })
    },
    bindPreviewImg(e) {
      const index = e.currentTarget.dataset.index
      const _this = this
      wx.previewImage({
        current: _this.data.imgList[index], // 当前显示图片的http链接
        urls: _this.data.imgList // 需要预览的图片http链接列表
      })
    },
    catchDelete(e) {
      const index = e.currentTarget.dataset.index
      const imgList = this.data.imgList
      const imgPath = this.data.imgPath
      imgList.splice(index, 1)
      imgPath.splice(index, 1)
      app.globalData.imgUrl.splice(index, 1)
      this.setData({
        imgList,
        imgPath
      })
    },
    // 上传图片
    uploadImg() {
      const time = this.data.imgPath.length
      wx.showToast({
        title: '正在上传',
        icon: 'loading',
        duration: 10000,
        mask: true
      })
      const imgList = this.data.imgList
      const imgPath = []
      const _this = this
      this.data.imgList.map((item, index) => {
        wx.uploadFile({
          url: `${base.pro}/common/fileUpload`,
          filePath:item,
          name: 'file',
          formData: {},
          success(res) {
            wx.hideToast()
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 1000
            })
            const data = res.data
            imgPath.push(data)
            _this.setData({
              imgPath
            })
            console.log(data)
            if(_this.properties.count > 1) {
              app.globalData.imgUrl = app.globalData.imgUrl.concat(data)
            } else {
              app.globalData.imgUrl.push(data)
            }
            console.log(app.globalData.imgUrl)
          }
        })
      })
    }
  }
})